package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.DayStatis;
import com.qf.vo.OrderAmtsVo;
import com.qf.vo.UserOrderCntVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 首页。仪表盘接口
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.02.21
 * @since 1.0
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/admin/dashboard")
public class DtsAdminController {

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsUserService userService;

    @Autowired
    DtsProductService productService;

    @Autowired
    DtsOrderService orderService;

    @Autowired
    DtsCategoryService categoryService;

    @GetMapping("/chart")
    public Object chart(){

        List<CategorySellAmts> categorySellAmtsList = categoryService.getCategorySellAmts();
        List<String> categoryNames = fetchCategoryName(categorySellAmtsList);
        Map<String,Object> categorySell = new HashMap<>();
        categorySell.put("categoryNames",categoryNames);
        categorySell.put("categorySellData",categorySellAmtsList);


        //统计最近一个月范围内 订单的总额和订单的笔数（包含一个月内的日期）
        List<DayStatis> orderDayStatis = orderService.findOrderDayStatis(30);
        //提取数据中的日期字符串
        String[] dayDate = fetchDayData(orderDayStatis);

        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();
        orderAmtsVo.setDayData(dayDate);

        //提取订单的笔数
       int[] orderCnt = fetchCnt(orderDayStatis,dayDate);

        orderAmtsVo.setOrderCntData(orderCnt);

        //提取订单总额
        BigDecimal[] orderAmt = fetchOrderAmt(orderDayStatis,dayDate);

        orderAmtsVo.setOrderAmtData(orderAmt);


        Map<String, Object> data = new HashMap<>();
        //大类 销售额统计
        data.put("categorySell",categorySell);
        //日期对应的订单总额 订单笔数
        data.put("orderAmts",orderAmtsVo);


        //查询一个月内 用户情况
        List<DayStatis> userDayStatis = userService.findUserDayStatis(30);

        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();
        //把user和order存储到一个数组中
       String[] userDayStr =  unionDayStr(orderDayStatis,userDayStatis);
        //提取用户的日增长量
        int[] cnts = fetchCnt(userDayStatis, userDayStr);
        userOrderCntVo.setDayData(userDayStr);

        userOrderCntVo.setUserCnt(cnts);
        //提取order的日增长量
        int[] orderCnts = fetchCnt(orderDayStatis, userDayStr);

        userOrderCntVo.setOrderCnt(orderCnts);


        //日期对应的用户数和订单数
        data.put("userOrderCnt",userOrderCntVo);

        return ResponseUtil.ok(data);
    }

    private String[] unionDayStr(List<DayStatis> orderDayStatis, List<DayStatis> userDayStatis) {
        Set<String> set = new HashSet<>();
        //把 User 和 Order 结果中的日期都添加到了set集合中
        if(orderDayStatis != null && orderDayStatis.size() >0){
           for(DayStatis dayStatis : orderDayStatis){
               set.add(dayStatis.getDayStr());
           }
        }
        if(userDayStatis != null && userDayStatis.size() >0) {
            for (DayStatis dayStatis : userDayStatis) {
                set.add(dayStatis.getDayStr());
            }
        }
        //把set集合转换为list集合
        ArrayList<String> list = new ArrayList<>(set);
        Collections.sort(list);
        return list.toArray(new String[]{});
    }

    private BigDecimal[] fetchOrderAmt(List<DayStatis> orderDayStatis, String[] dayDate) {
       BigDecimal[] arr = new BigDecimal[dayDate.length];
        if(orderDayStatis != null && orderDayStatis.size() >0){
            for (int i = 0; i < dayDate.length; i++) {
                for (DayStatis dayStatis : orderDayStatis) {
                    String dayStr = dayStatis.getDayStr();
                    if(dayDate[i].equalsIgnoreCase(dayStr)){
                        arr[i] = dayStatis.getAmts();
                        break;
                    }
                }
            }
        }
        return arr;
    }

    private int[] fetchCnt(List<DayStatis> orderDayStatis, String[] dayDate) {
        int[] arr = new int[dayDate.length];
        if(orderDayStatis != null && orderDayStatis.size() >0){
            for (int i = 0; i < dayDate.length; i++) {
                for (DayStatis dayStatis : orderDayStatis) {
                    String dayStr = dayStatis.getDayStr();
                    if(dayDate[i].equalsIgnoreCase(dayStr)){
                        arr[i] = dayStatis.getCnts();
                        break;
                    }
                }
            }
        }
        return arr;
    }

    private String[] fetchDayData(List<DayStatis> orderDayStatis) {

        List<String> dayList = new ArrayList<>();
        //提取日期字符串，存储到集合中
        if(orderDayStatis !=null && orderDayStatis.size() > 0){
            for (DayStatis dayStatis : orderDayStatis) {
                String dayStr = dayStatis.getDayStr();
                dayList.add(dayStr);
            }
        }
        //升序排序
        Collections.sort(dayList);
        //把集合转换成数组
        String[] dayArr= dayList.toArray(new String[]{});

        return  dayArr;

    }

    private List<String> fetchCategoryName(List<CategorySellAmts> categorySellAmtsList) {
        //创建返回值对象
        List<String> names = new ArrayList<>();

        //遍历集合，取出所有的names,存储到集合中，并返回
        if(categorySellAmtsList !=null && categorySellAmtsList.size() > 0){
            for (CategorySellAmts categorySellAmts : categorySellAmtsList) {

                names.add( categorySellAmts.getName());
            }
        }
        return names;
    }

    @GetMapping
    public Object info(){
        int goodsTotal = goodsService.queryTotal();
        int userTotal = userService.queryTotal();
        int productTotal = productService.queryTotal();
        int orderTotal = orderService.queryTotal();

        Map<String,Object> data = new HashMap<>();
        data.put("goodsTotal",goodsTotal);
        data.put("userTotal",userTotal);
        data.put("productTotal",productTotal);
        data.put("orderTotal",orderTotal);

        return ResponseUtil.ok(data);
    }

}
